package com.twm.interceptor;


import com.twm.common.ResponseMessage;
import com.twm.common.StatusCode;
import com.twm.redis.RedisService;
import com.twm.util.CookieUtil;
import com.twm.util.HttpResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.twm.common.CommonEnum.LOGIN_COOKIE_T;
import static com.twm.common.CommonEnum.LOGIN_URL;

/**
 * @ClassName TimeOutInterceptor
 * @Description
 * @Author twm17
 * @Date 2020/8/13 10:10
 * @Version 1.0
 */
@Component
public class TimeOutInterceptor implements HandlerInterceptor {
    /**
     * 对超时进行拦截
     * @param request 1
     * @param response 2
     * @param handler 3
     * @return boolean
     * @author twm
     * @date 2020/8/13
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

        if (request.getRequestURI().contains(LOGIN_URL)) {
            return true;
        }
        if (CookieUtil.existsCookie(request,LOGIN_COOKIE_T)) {
            return true;
        }
        HttpResponseUtil.writeResponseMessage(response,
                new ResponseMessage(StatusCode.REDIRECT_LOGIN.getStatus(), StatusCode.REDIRECT_LOGIN.getMessage()));
        return  false;
    }
}
